//
//  LeeGoodsListViewController.m
//  firstpoint
//
//  Created by 李雪虎 on 2017/7/3.
//  Copyright © 2017年 xzcysoft. All rights reserved.
//

#import "LeeGoodsListViewController.h"
#import "LeeShoppingTableViewCell.h"
@interface LeeGoodsListViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,UITextFieldDelegate>
{
    int page;
    DontHaveDataView*dontData;
    
}

@property(nonatomic,strong)UITextField*SSSRTextField;//输入框
@property(nonatomic,strong)UITableView *ShoppingTabelView;//表
@property(nonatomic,strong)NSMutableArray *shoppingArr;
@end

@implementation LeeGoodsListViewController
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden=NO;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    page=1;
    _shoppingArr = [NSMutableArray array];
    [self httpShoppingURL];//搜索URL
    self.title = @"搜索结果";
    self.view.backgroundColor = [UIColor whiteColor];
    [self addsousuo];//搜索框
    [self addShoppingMallTableView];//添加表
    dontData=[[DontHaveDataView alloc]initWithFrame:CGRectMake(0, 104, width_screen, height_screen-104)];
    dontData.hidden=YES;
    [self.view addSubview:dontData];
    // Do any additional setup after loading the view.
}
//搜索框
-(void)addsousuo{
    UIView*NVView =[[UIView alloc]init];
    NVView.frame=CGRectMake(0, 64, width_screen, 40);
    NVView.backgroundColor = COLOR(212, 217, 220, 1);
    [self.view addSubview:NVView];
    //输入框背景
    UIView * sosuobgView = [[UIView alloc]initWithFrame:CGRectMake(10, 5, width_screen-20 , 30)];
    sosuobgView.backgroundColor = [UIColor whiteColor];
    [sosuobgView.layer setMasksToBounds:YES];//编辑
    [sosuobgView.layer setCornerRadius:4.0];//圆角度
    [NVView addSubview:sosuobgView];
    //搜索按钮
    UIButton *SSbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    SSbutton.frame = CGRectMake(4, 2.5, 25, 25);
    [SSbutton setImage:[UIImage imageNamed:@"Imported Layers"] forState:UIControlStateNormal];
    [sosuobgView addSubview:SSbutton];
    //搜索输入框
    _SSSRTextField = [[UITextField alloc]initWithFrame:CGRectMake(30, 1, 461, 29)];
    _SSSRTextField.placeholder = @"请输入任务或商品名称";
    _SSSRTextField.text = _shoppingName;
    _SSSRTextField.font = [UIFont systemFontOfSize:13];
    [_SSSRTextField setValue:COLOR(187, 187, 187, 1) forKeyPath:@"_placeholderLabel.textColor"];
    _SSSRTextField.returnKeyType = UIReturnKeySearch;
    _SSSRTextField.returnKeyType=UIReturnKeySearch;
    _SSSRTextField.delegate = self;
    [sosuobgView addSubview:_SSSRTextField];
}
//添加表视图
-(void)addShoppingMallTableView{
    //表格
        _ShoppingTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 104, width_screen, height_screen-64-40) style:UITableViewStyleGrouped];
    _ShoppingTabelView.backgroundColor = [UIColor clearColor];
    _ShoppingTabelView.showsVerticalScrollIndicator = NO;//隐藏滑动条
    //    _tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _ShoppingTabelView.separatorColor = COLOR(229, 230, 231, 1);//分割线颜色
    _ShoppingTabelView.delegate = self;
    _ShoppingTabelView.dataSource = self;
    //    _tabelView.bounces = NO;
    //    [_tabelView registerClass:[DongTaiTableViewCell class] forCellReuseIdentifier:@"cellOen"];
    [_ShoppingTabelView registerNib:[UINib nibWithNibName:@"LeeShoppingTableViewCell" bundle:nil] forCellReuseIdentifier:@"OenCell"];
    //    [_tabelView registerClass:[DingYueWenXianTableViewCell class] forCellReuseIdentifier:@"cellTwo"];
    //    _ShoppingTabelView.scrollEnabled = NO;
    [self.view addSubview:_ShoppingTabelView];
   
}
#pragma mark --tableView 代理方法
//区头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
}

//多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _shoppingArr.count;
}
//几个区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//区尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 40;
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LeeShoppingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OenCell"];
    if (_shoppingArr.count>1) {
        cell.titleLabel.text = [NSString stringWithFormat:@"%@",_shoppingArr[indexPath.row][@"goodName"]];//标题
        
        cell.YBLabel.text = [NSString stringWithFormat:@"%@ 元宝",_shoppingArr[indexPath.row][@"ingot"]];//元宝
        
        cell.JBLabel.text = [NSString stringWithFormat:@"%@ 金币",_shoppingArr[indexPath.row][@"gold"]];//金币
        
        cell.soldLabel.text = [NSString stringWithFormat:@"已售 %@",_shoppingArr[indexPath.row][@"count"]];//已售
        //        cell.ImgView.image = [UIImage imageNamed:_renwuArray[indexPath.row][@"fileId"]];//图片
    }
    return cell;
    
}




#pragma mark--键盘搜索事件
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"点击了搜索");
    [self.view endEditing:YES];
    _shoppingName = textField.text;
    [self httpShoppingURL];
    return YES;
}
//cell 分割线 两端封头 实现这个两个方法 1
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
-(void)httpShoppingURL{
    
    dontData.hidden=YES;

    __weak typeof(self) weakSelf = self;
    [HTTPURL getRequest:LINK_BASE_URL(@"goods/searchGoodsByGoodName.api") parameters:@{@"goodName":_shoppingName,@"pageIndex":@(page)} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        
        if (![self judgeIsEmptyWithString:responseObject[@"data"]]) {
            NSDictionary *dic = [PublicTools dictionaryWithJsonString:responseObject[@"data"]];
            NSArray *array = [NSArray array];
            array = (NSArray *)dic[@"rows"];
            [_shoppingArr addObjectsFromArray:array];
            [weakSelf.ShoppingTabelView reloadData];
        }else{
        
            dontData.hidden=NO;
        }
        
    } filure:^(NSURLSessionDataTask *task, id error) {
        NSLog(@"%@",error);
    }];
}
-(BOOL)judgeIsEmptyWithString:(NSString *)string
{
    if ([string isEqual:[NSNull null]]||string.length == 0 || [string isEqualToString:@""] || string == nil || string == NULL )
    {
        return YES;
    }
    return NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
